package com.bwhx.eruler.controller.casdilly;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bwhx.eruler.bean.casdilly.ClothesTypeExport;
import com.bwhx.eruler.controller.BaseController;
import com.bwhx.eruler.service.casdilly.IClothesTypeExportService;
import com.bwhx.eruler.util.StringUtils;

@Controller
@RequestMapping("/clothesTypeExport")
public class ClothesTypeExportController extends BaseController {

	@Autowired
	IClothesTypeExportService clothesTypeExportService;

	@ResponseBody
	@RequestMapping(value = "/page.initclothestypeexportconfig.ashx", method = RequestMethod.POST)
	public Map<String, Object> getClothesTypeExportConfig(HttpServletRequest reqeust, HttpServletResponse response,
			String categoryName) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<ClothesTypeExport> exports = clothesTypeExportService.getClothesTypeExportByCategoryName(categoryName);
		map.put("data", exports);
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/page.saveclothestypeexportconfig.ashx", method = RequestMethod.POST)
	public Map<String, Object> saveClothesTypeExportConfig(HttpServletRequest reqeust, HttpServletResponse response,
			String data, String categoryName) {
		Map<String, Object> map = new HashMap<String, Object>();
		ClothesTypeExport entity = new ClothesTypeExport();
		entity.settExportTypeName(categoryName);
		clothesTypeExportService.delete(entity);
		String str = "";
		if (data != null && !data.isEmpty()) {
			List<ClothesTypeExport> items = new ArrayList<ClothesTypeExport>();
			String[] strings = data.split(",");
			String[] array = null, values = null;
			for (int i = 0; i < strings.length; i++) {
				values = strings[i].split(":");
				if (values.length > 1) {
					array = values[0].split("_");
					if (array.length > 1) {
						str = array[0];
						int index = -1;
						for (int j = 0; j < items.size(); j++) {
							if (items.get(j).gettExportItemName().equals(array[0])) {
								index = j;
								break;
							}
						}
						if (index > -1) {
							if (array[1].equals("data")) {
								items.get(index).settIsExportItemValue(true);
							} else {
								items.get(index).settIsExportItemCorrectValue(true);
							}							
							items.get(index).settExportOrder(StringUtils.tryParseInt(values[1]));

						} else {
							ClothesTypeExport item = new ClothesTypeExport();
							item.settExportId(UUID.randomUUID().toString().replace("-", ""));
							item.settExportTypeName(categoryName);
							item.settExportItemName(str);
							item.settExportOrder(StringUtils.tryParseInt(values[1]));
							if (array[1].equals("data")) {
								item.settIsExportItemValue(true);
							} else {
								item.settIsExportItemCorrectValue(true);
							}
							items.add(item);
						}
					}
				}
			}
			try {				
				str = clothesTypeExportService.SaveClothesTypeExports(items);
				str = "保存成功";
			} catch (Exception e) {
				str = e.getMessage();
			}
			map.put("msg", str);
		}
		return map;
	}

	
}
